﻿Ext.define('passport.controller.DomainObjectController',{
    extend: 'Ext.app.Controller',

    stores: ['DomainObjectStore'],
		/**定义Model*/
    model: ['passport.model.DomainObjectModel'],

    views: ['domainObject.DomainObjectEditView', 'domainObject.DomainObjectListView'],

    refs: [
        {
            ref: 'domainObjectsPanel',
            selector: 'panel'
        }
    ],

    init: function() {
        this.control({
        	
            'domainObjectEditView button[action=save]': {
                click: this.updateDomainObject
            },
						'domainObjectListView':{
							itemdblclick: this.editDomainObject
						},
						'domainObjectListView button[action=create]':{
							click: this.createDomainObject
						},
						'domainObjectListView button[action=delete]':{
							click: this.deleteDomainObject
						},
						'domainObjectListView button[action=refresh]':{
							click: this.refreshDomainObject
						},
						'productTree':{
							itemclick: this.refreshDomainObjectByModule
						},
						'permissionTree':{
							itemclick: this.refreshDomainObjectBySysGroup
						}
        });
    }
    ,

    editDomainObject: function(grid, record) {
        var edit = Ext.create('passport.view.domainObject.DomainObjectEditView').show();
        edit.down('form').loadRecord(record);
    },

    updateDomainObject: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();

       if (form.getForm().isValid()){
	        record.set(values);
	        var store = this.getStore('DomainObjectStore');
	        record.save({
	        		success: function() {
	        			store.load();
	        			win.close();
	        		},
	        		failure : function() {
	        		
					Ext.Msg.alert("failed to save DomainObject!");
				}
	        	});
	        
      	} else {
      		console.debug('Error occured while saving SysMenu');
      	}
    },
    
    createDomainObject: function(grid) {
    	var edit = Ext.create('passport.view.domainObject.DomainObjectEditView').show();
    	var record = Ext.create('passport.model.DomainObjectModel');
      edit.down('form').loadRecord(record);
    },
    
    deleteDomainObject: function(button) {
    	var gridPanel = button.up("grid");
    	var records = gridPanel.getSelectionModel();
    	var record = records.getSelection()[0];
    	var store = this.getStore('DomainObjectStore');
    	record.destroy({
	        		success: function() {
	        			store.load();
	        			console.log("delete object success!");	
	        		},
	        		failure : function() {
	        			console.log("failed to save the object");
	        		}
	        	});

    	console.log('delete');
    },
    
    refreshDomainObject: function() {
    	this.getStore('DomainObjectStore').load();
    },
    
    refreshDomainObjectByModule: function(node, e){
	    console.log(e.data.id);
	    if(e.data.leaf){
			var store=this.getStore('DomainObjectStore');
			store.load({params:{filter:'getDomainObjectByModule', moduleId: e.data.id} });
		}	    
    },
    
    refreshDomainObjectBySysGroup: function(node, e){
	    console.log(e.data.id);
	    if(e.data.leaf){
			var store=this.getStore('DomainObjectStore');
			store.load({params:{filter:'getDomainObjectBySysGroup', sysGroupId: e.data.id} });
		}	    
    }
});
